package com.ruoyi.quartz.task;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bizsys.domain.*;
import com.ruoyi.bizsys.mapper.*;
import com.ruoyi.bizsys.service.ICustomerInfoService;
import com.ruoyi.business.service.ICustVaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 查询提现成功后，没有生成VA账户的
 * createVaTask.runCreateVa()
 *
 * @author ruoyi
 */
@Slf4j
@Component("createVaTask")
public class CreateVaTask {
    @Autowired
    ExtMonnifyReservedAccountsMapper extMonnifyReservedAccountsMapper;
    @Autowired
    ExtPaystackCustVaResultMapper extPaystackCustVaResultMapper;
    @Autowired
    ICustVaService delCustVaService;

    @Autowired
    ICustomerInfoService customerInfoService;
    @Autowired
    ExtPaystackReservedAccountsMapper extPaystackReservedAccountsMapper;

    //@PostConstruct
    public void runCreateVa() {
        // select DISTINCT cust_no from loan_iou a where NOT EXISTS (select DISTINCT cust_no from ext_monnify_reserved_accounts b where a.cust_no = b.cust_no);
        // 查询提现后没有生成VA的客户号
        List<ExtMonnifyReservedAccounts> list = extMonnifyReservedAccountsMapper.getCustNoNotInVa();
        log.info("createVaTask:size{}", list.size());
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            String userId = customerInfoService.getUserIdByCustNo(list.get(i).getCustNo());
            delCustVaService.delCustVaForPaystack(list.get(i).getCustNo(), userId);
            delCustVaService.delCustVaForMonnify(list.get(i).getCustNo());
        }
        List<ExtPaystackCustVaResult> listPayStack = extPaystackCustVaResultMapper.selectList(
                new QueryWrapper<ExtPaystackCustVaResult>().eq("status", 3));
        if (CollectionUtils.isEmpty(listPayStack)) {
            return;
        }
        for (int i = 0; i < listPayStack.size(); i++) {
            String userId = customerInfoService.getUserIdByCustNo(listPayStack.get(i).getCustNo());
            delCustVaService.delCustVaForPaystack(listPayStack.get(i).getCustNo(), userId);
        }

    }


    public void runCreatePaystackVa() {
        // 需要的是历史借据未结清的客户，查出有没有paystack VA 没有生生成的
        List<ExtPaystackReservedAccounts> list = extPaystackReservedAccountsMapper.getCustNoNotInPaystackVa();
        log.info("createVaTask:size{}", list.size());
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            try {
                Thread.sleep(500);
                String userId = customerInfoService.getUserIdByCustNo(list.get(i).getCustNo());
                delCustVaService.delCustVaForPaystack(list.get(i).getCustNo(), userId);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }


}